﻿#include "PlatformProperty.h"

MAPPING_VALUE(sdb::AvailabilitySts, PropertyStatus, PropertyStatus::NotAvailable)
MAPPING_ITEM(sdb::AvailabilitySts::Active, PropertyStatus::Active)
MAPPING_ITEM(sdb::AvailabilitySts::NotActive, PropertyStatus::NotActive)
MAPPING_ITEM(sdb::AvailabilitySts::Error, PropertyStatus::Error)
MAPPING_ITEM(sdb::AvailabilitySts::NotAvailable, PropertyStatus::NotAvailable)
MAPPING_END

float temMinSet(int unit) {
    if (unit == toInt(sdb::AmbTIndcdUnit::Fahrenheit)) {
        return 59.0f;
    } else {
        return 15.5f;
    }
}

float temMaxSet(int unit) {
    if (unit == toInt(sdb::AmbTIndcdUnit::Fahrenheit)) {
        return 85.0f;
    } else {
        return 28.5f;
    }
}

float valueBySignalsSet(int areaId, float last, int unint) {
    int data = 0;
    if (areaId == static_cast<int32_t>(VehicleAreaSeat::ROW_1_LEFT)) {
        data = SIGNAL.getPA_CL_LeftTemp().data;
    } else if (areaId == static_cast<int32_t>(VehicleAreaSeat::ROW_1_RIGHT)) {
        data = SIGNAL.getPA_CL_RightTemp().data;
    }
    float temp;
    if (unint == toInt(VehicleUnit::FAHRENHEIT)) {
        temp = data + 58;
    } else {
        temp = data * 0.5 + 15;
    }
    VHALOG.d("HVAC_TEMPERATURE_SET unint is : %d, value changed to: %d ，areid is : %d", unint, data ,areaId);
    return temp;
}

void setProperty(int areaId, float value, int unint) {
    int data = 0;
    if (unint == toInt(VehicleUnit::FAHRENHEIT)) {
        data = value - 58;
    } else {
        data = (value - 15) * 2;
    }
    //当设置温度超过最大值或者低于最小值时，下发最大最小值    
    if( data <= 1) {
        data = 1;
    } else if (data >= 27) { 
        data = 27;
    }
    

    if (areaId == static_cast<int32_t>(VehicleAreaSeat::ROW_1_LEFT)) {
        SIGNAL.setCB_CL_LeftTemp(data);
    } else if (areaId == static_cast<int32_t>(VehicleAreaSeat::ROW_1_RIGHT)) {
        SIGNAL.setCB_CL_RightTemp(data);
    }
    VHALOG.d("HVAC_TEMPERATURE_SET unint is : %d, value changed to: %d ，areid is : %d", unint, data ,areaId);
}

SYSTEM_READ_WRITE_PROPERTY(HVAC_TEMPERATURE_SET, float) {
    builder.area(VehicleAreaSeat::ROW_1_LEFT)
        .initValue([this]() {
            int unint = getPropertyValue<int32_t>(
                static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                static_cast<int32_t>(VehicleArea::GLOBAL));
            if (unint == toInt(VehicleUnit::FAHRENHEIT)) {
                return 72.0f;
            } else {
                return 22.0f;
            }
        })
        .withStatusBySignals(
            []([[maybe_unused]] const PropertyStatus& lastStatus) {
                sdb::AvailabilitySts availabilitySts =
                    sdb::AvailabilitySts(SIGNAL.getPA_CL_LeftTemp().availability);
                PropertyStatus status = mappingValue(availabilitySts, PropertyStatus::NotAvailable);
                VHALOG.e("HVAC_TEMPERATURE_SET row_1_left status changed to: %d ", status);
                return status;
            },
            PASignal::PA_CL_LeftTemp)
        .withValueBySignals(
            [this]([[maybe_unused]] const float& last) {
                int unint = getPropertyValue<int32_t>(
                    static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                    static_cast<int32_t>(VehicleArea::GLOBAL));
                return valueBySignalsSet(static_cast<int32_t>(VehicleAreaSeat::ROW_1_LEFT), last,
                                      unint);
            },
            PASignal::PA_CL_LeftTemp)
        .onSetProperty([this](const float& value) {
            int unint = getPropertyValue<int32_t>(
                static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                static_cast<int32_t>(VehicleArea::GLOBAL));
            VHALOG.d("HVAC_TEMPERATURE_SET drvr: onSetProperty %f", value);
            setProperty(static_cast<int32_t>(VehicleAreaSeat::ROW_1_LEFT), value, unint);
            setValue(static_cast<int32_t>(VehicleAreaSeat::ROW_1_LEFT), value, false);
        })
        .area(VehicleAreaSeat::ROW_1_RIGHT)
        .initValue([this]() {
            int unint = getPropertyValue<int32_t>(
                static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                static_cast<int32_t>(VehicleArea::GLOBAL));
            if (unint == toInt(VehicleUnit::FAHRENHEIT)) {
                return 72.0f;
            } else {
                return 22.0f;
            }
        })
        .withStatusBySignals(
            []([[maybe_unused]] const PropertyStatus& lastStatus) {
                sdb::AvailabilitySts availabilitySts =
                    sdb::AvailabilitySts(SIGNAL.getPA_CL_RightTemp().availability);
                PropertyStatus status = mappingValue(availabilitySts, PropertyStatus::NotAvailable);
                VHALOG.d("HVAC_TEMPERATURE_SET row_1_right status changed to: %d ", status);
                return status;
            },
            PASignal::PA_CL_RightTemp)
        .withValueBySignals(
            [this]([[maybe_unused]] const float& last) {
                int unint = getPropertyValue<int32_t>(
                    static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                    static_cast<int32_t>(VehicleArea::GLOBAL));
                return valueBySignalsSet(static_cast<int32_t>(VehicleAreaSeat::ROW_1_RIGHT), last,
                                      unint);
            },
            PASignal::PA_CL_RightTemp)
        .onSetProperty([this](const float& value) {
            int unint = getPropertyValue<int32_t>(
                static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                static_cast<int32_t>(VehicleArea::GLOBAL));
            VHALOG.d("HVAC_TEMPERATURE_SET pass: onSetProperty %f", value);
            setProperty(static_cast<int32_t>(VehicleAreaSeat::ROW_1_RIGHT), value, unint);
            setValue(static_cast<int32_t>(VehicleAreaSeat::ROW_1_RIGHT), value, false);
        });
}
